//
//  TJTitleBtn.m
//  TJSinaWeiBo_OC
//
//  Created by 唐后能 on 16/4/27.
//  Copyright © 2016年 itheima. All rights reserved.
//

#import "TJTitleBtn.h"

@implementation TJTitleBtn

-(UIButton *)initWithImgName:(NSString*)imgName andTitle:(NSString*)title andTarget:(id) target action:(SEL)action {

    if (self = [super initWithFrame:CGRectZero]) {
 
//        self.imageView.image = [UIImage imageNamed:imgName];
//        self.titleLabel.text = title;
#pragma btn的image和title都是有状态的,必须用set方法来设置,否则不会显示.
        [self setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
        [self setTitle:title forState:UIControlStateNormal];
        [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self sizeToFit];
        // 添加点击事件.
        [self addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

// 改变布局
-(void)layoutSubviews {

    [super layoutSubviews];
    // 布局titleLabel.
    CGRect frame = self.titleLabel.frame;
    frame.origin.x = 0;
    self.titleLabel.frame = frame;
    
    // 布局imageView.
    frame = self.imageView.frame;
    frame.origin.x = self.titleLabel.frame.size.width + 5;
    self.imageView.frame = frame;

}

@end
